//
//  DetailViewController.m
//  PlayingWithMaps
//
//  Created by Jaime on 6/24/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "DetailViewController.h"
#import "RootViewController.h"

#include "UIUtils.h"
#include "ControlAnnotation.h"
#include "ControlAnnotationView.h"

@interface DetailViewController ()
@property (nonatomic, retain) UIPopoverController *popoverController;
- (void)configureView;
@end



@implementation DetailViewController

@synthesize toolbar, popoverController, detailItem, detailDescriptionLabel, place, mapPlace;

#pragma mark -
#pragma mark Managing the detail item

/*
 When setting the detail item, update the view and dismiss the popover controller if it's showing.
 */

- (void)setMapPlace:(MapPlace*)newPlace {
	if( mapPlace != newPlace) {
		[mapPlace release];
		mapPlace = [newPlace retain];
		
		[self configureView];
	}

    if (popoverController != nil) {
        [popoverController dismissPopoverAnimated:YES];
	}
}

- (void)configureView {
    // Update the user interface for the detail item.
    detailDescriptionLabel.text = [mapPlace getName];
	
	MKCoordinateRegion region = [self getRegion:[self.mapPlace getLocation]];
	[mapView setRegion:region];
	[mapView regionThatFits:region];
}


#pragma mark -
#pragma mark Split view support

- (void)splitViewController: (UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController: (UIPopoverController*)pc {
    
    barButtonItem.title = @"Root List";
    NSMutableArray *items = [[toolbar items] mutableCopy];
    [items insertObject:barButtonItem atIndex:0];
    [toolbar setItems:items animated:YES];
    [items release];
    self.popoverController = pc;
}


// Called when the view is shown again in the split view, invalidating the button and popover controller.
- (void)splitViewController: (UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
    
    NSMutableArray *items = [[toolbar items] mutableCopy];
    [items removeObjectAtIndex:0];
    [toolbar setItems:items animated:YES];
    [items release];
    self.popoverController = nil;
}


#pragma mark -
#pragma mark Rotation support

// Ensure that the view controller supports rotation and that the split view can therefore show in both portrait and landscape.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}


#pragma mark -
#pragma mark View lifecycle


- (MKCoordinateRegion) getRegion: (CLLocationCoordinate2D) location  {

	MKCoordinateSpan span;
	
	span.latitudeDelta = 0.2;
	span.longitudeDelta = 0.2;
	
	MKCoordinateRegion region;
	region.span = span;
	region.center = location;
	
	return region;

}
- (void) setRegionOnMap:(MKMapView*) map {


}
 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
	mapView.userInteractionEnabled = TRUE;

	CLLocationCoordinate2D loc = [mapPlace getLocation];
	MKCoordinateRegion region = [self getRegion:loc];
	[mapView setRegion:region];
	[mapView regionThatFits:region];
	
	// add the map to the view
	[self.view insertSubview:mapView atIndex:0];
}

- (void) addAnnotationAtCenter {
	
	ControlAnnotation* an = [[ControlAnnotation alloc] init];
	an.title = @"control";
	an.coordinate = mapView.centerCoordinate;
	
	[mapView addAnnotation:an];
	
}

- (void) cameraButtonPressed {
	[self addAnnotationAtCenter];
}

- (void) myAddButtonPressed {
	[self addAnnotationAtCenter];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	
	UIBarButtonItem* addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(myAddButtonPressed)];
	UIBarButtonItem* someButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(cameraButtonPressed)];
	UIBarButtonItem* cameraButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(cameraButtonPressed)];
	UIBarButtonItem* spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
	UIBarButtonItem* flexSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
//	self.navigationItem.leftBarButtonItem = addButton;
	
	NSArray* items = [NSArray arrayWithObjects:spacer, someButton, spacer, cameraButton, flexSpacer, addButton, nil];
	
	[toolbar setItems:items];
	
	[addButton release];
	[cameraButton release];
	[someButton release];
	[flexSpacer release];
	[spacer release];

}


/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.popoverController = nil;
}


#pragma mark -
#pragma mark MapViewDelegates

- (MKAnnotationView*) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation {
	// if its the user localtiuon then return nil, we don't handle the map's userLocaltion annotation
	if([annotation isKindOfClass:[MKUserLocation class]])
		return nil;
	
	// try to get an annotation view from the queue or init a new one of non exists
	ControlAnnotationView* annotationView = (ControlAnnotationView*) [theMapView dequeueReusableAnnotationViewWithIdentifier:@"Pin"];
	if( annotationView == nil)	{
		annotationView = [[[ControlAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"] autorelease];
	}
	
	// assign the map to the view so we can handle the touch events
	annotationView.mapView = theMapView;
	
	return annotationView;
}


#pragma mark -
#pragma mark Memory management

/*
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}
*/

- (void)dealloc {
    [popoverController release];
    [toolbar release];
	[mapPlace release];
    
    [detailItem release];
    [detailDescriptionLabel release];
    [super dealloc];
}

@end
